package com.yangyouli.unity_demo_server.controller;

import com.yangyouli.unity_demo_server.annotation.Inject;
import com.yangyouli.unity_demo_server.annotation.ResponseClass;
import com.yangyouli.unity_demo_server.annotation.ResponseMethod;
import com.yangyouli.unity_demo_server.entity.UserData;
import com.yangyouli.unity_demo_server.server.Client;
import com.yangyouli.unity_demo_server.server.RoomObj;
import com.yangyouli.unity_demo_server.service.UserDataService;
import com.yangyouli.unity_demo_server.util.ThreadLocalHelper;

@ResponseClass("inGamePlayer")
public class InGamePlayerController {
	@Inject
	private UserDataService userDataService;
	
	/**
	 * @param data 1:游戏开始血量初始化，2:复活血量初始化
	 * @return
	 */
	@ResponseMethod("initPlayerHp")
	public String initPlayerHp(String data) {
		Client client = ThreadLocalHelper.getClient();
		UserData userData = userDataService.findByUserId(client.getUserId());
		if ("2".equals(data)) { // 复活初始化
			if (userData.getMoney() >= 10) {
				userData.setMoney(userData.getMoney() - 10);
				userDataService.UpdateNumbricFieldByUserId(userData);
			} else {
				return "0,金钱不足10无法复活";
			}
		}
		int level = userData.getLevel();
		int hp = level + 100;
		client.setHp(hp);
		return "1," + hp;
	}
	
	@ResponseMethod("damagePlayer")
	public String damagePlayer(String data) {
		Client client = ThreadLocalHelper.getClient();
		int damage = Integer.valueOf(data);
		client.setHp(client.getHp() - damage);
		return client.getUserId() + "," + client.getHp();
	}
	
	@ResponseMethod("getAlivePlayerNum")
	public String getAlivePlayerNum(String data) {
		RoomObj roomObj = ThreadLocalHelper.getClient().getRoomObj();
		return ThreadLocalHelper.getClient().getUserId() + "," + roomObj.getAlivePlayerNum();
	}
	
	@ResponseMethod("addHp")
	public String addHp(String data) {
		Client client = ThreadLocalHelper.getClient();
		int addHp = Integer.valueOf(data);
		client.setHp(client.getHp() + addHp);
		return client.getHp() + "";
	}
	
	
	
	
	
}
